library(tidyverse)

#please download the UNGA  Data from here: https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/LEJUQZ (as of 17.11.2023). We used the June 2022 data.
#the data is based on Bailey, Michael A., Anton Strezhnev, and Erik Voeten. "Estimating dynamic state preferences from United Nations voting data." Journal of Conflict Resolution 61.2 (2017): 430-456.

unga <- read.csv("./nonesharable_data/AgreementScoresAll_Jun2022.csv")
cow <- read.csv("./nonesharable_data/COW-country-codes.csv") %>%
  select(country_short = 1, country_code = 2) %>% 
  distinct()

unga <- dplyr::left_join(unga, cow %>% 
                           select(country_short1 = 1, ccode1 = 2)) %>% 
  dplyr::left_join(., cow %>% 
                     select(country_short2 = 1, ccode2 = 2)) %>% 
  dplyr::select(year, country_short1, country_short2, dist = IdealPointDistance) %>% 
  dplyr::filter(country_short1 == "USA", year > 1998) %>% 
  arrange(country_short2, year) %>% 
  mutate(dist1=dplyr::lag(dist),
         dist2=dplyr::lag(dist,2),
         dist_avg=(dist+dist1+dist2)/3)

saveRDS(object = unga, file = "./nonesharable_data/unga_clean.RDS")
